package com.hzmg.discovery;

import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.AbstractBehavior;
import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive;
import com.hzmg.route.PoolRouteConversion;
import com.hzmg.route.RouteDefinition;
import com.hzmg.support.util.NameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DiscoveryPositioner extends AbstractBehavior<RouteDefinition> {
    private static final Logger logger = LoggerFactory.getLogger(DiscoveryPositioner.class);

    public DiscoveryPositioner(ActorContext<RouteDefinition> context) {
        super(context);
    }

    public static Behavior<RouteDefinition> create() {
        return Behaviors.setup(context -> new DiscoveryPositioner(context));
    }

    @Override
    public Receive<RouteDefinition> createReceive() {
        return newReceiveBuilder()
                .onMessage(RouteDefinition.class, this::onPosition)
                .build();
    }

    private Behavior<RouteDefinition> onPosition(RouteDefinition routeDefinition) {
        logger.info("[akkacloud-gateway-core] DiscoveryPositioner: {}", routeDefinition);

        ActorRef<?> actorRef = getContext().spawn(PoolRouteConversion.getInstance().routingBehavior(routeDefinition)
                , NameUtils.normalizeActorRefSimpleName(DiscoveryPositioner.class));

        return Behaviors.empty();
    }
}
